package com.example.springlearn.interceptor;

import com.example.springlearn.util.CookieUtil;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginEdInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        Cookie[] cookies = request.getCookies();
        if (cookies == null) {
            return true;
        }

        Cookie username = CookieUtil.getCookieByKey(cookies, "username");
        Cookie uid = CookieUtil.getCookieByKey(cookies, "uid");

        if (username == null || uid == null) {
            return true;
        }

        response.sendRedirect("/home/space");

        return false;
    }
}
